﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BasicForms;
using System.Data.SqlClient;

namespace ReceivingDelivering.ReportGenerators
{
    public partial class FrmReportProductReceiving : FrmSupportPrintComponents
    {
        public FrmReportProductReceiving()
        {
            InitializeComponent();
            cmbStaff.DataSource = Lists.DataAccess.ToDataTable(DB.Staffs.ToList()).DefaultView;
            cmbStaff.AddVisibleColumn("StaffNo", "Staff No", 100, 0);
            cmbStaff.AddVisibleColumn("StaffName", "Staff Name", 300, 1);
            cmbStaff.ValueColumn = "StaffID";
            cmbStaff.DisplayColumn = "StaffName";
        }

        protected override void LoadDataReport()
        {
            base.LoadDataReport();
            var dataSouce = DB.ReceivingBillInfos.Select(p => new
            {
                CreateDate = p.ReceivingBill.ReceivingCreateDate,
                ProductNo = p.Product.ProductNo,
                ProductName = p.Product.ProductName,
                p.ReceivingBill.Staff.StaffID,
                p.ReceivingBill.Staff.StaffName,
                Quantity = p.ReceivingQuantity,
                UnitPrice = p.Product.ReceivingPrice,
                ToMoney = p.Product.ReceivingPrice * p.ReceivingQuantity
            }).Where(p => p.CreateDate >= dtNgayBatDau.DateTime && p.CreateDate <= dtNgayKetThuc.DateTime
                                        && ((cmbStaff.EditValue != null) ? (p.StaffID == (int?)cmbStaff.EditValue) : (true)));

            System.Collections.Hashtable htContainer = new System.Collections.Hashtable();
            DataTable dtSource = new DataTable();
            dtSource.Columns.Add("style", typeof(string));
            dtSource.Columns.Add("stt", typeof(string));
            dtSource.Columns.Add("CreateDate", typeof(DateTime));
            dtSource.Columns.Add("ProductNo", typeof(string));
            dtSource.Columns.Add("ProductName", typeof(string));
            dtSource.Columns.Add("Quantity", typeof(decimal));
            dtSource.Columns.Add("UnitPrice", typeof(decimal));
            dtSource.Columns.Add("ToMoney", typeof(decimal)).DefaultValue = 0;
            DataRow rowTongCong = dtSource.NewRow();
            rowTongCong["ProductNo"] = "Toltal";
            rowTongCong["style"] = "bold";
            DataRow row = dtSource.NewRow();
            int stt = 0;
            foreach (var item in dataSouce)
            {
                //Insert cha
                if (!htContainer.Contains(item.StaffID))
                {
                    row = dtSource.Rows.Add("bold", ++stt, DBNull.Value, item.StaffName, "", DBNull.Value, DBNull.Value, 0);
                    htContainer.Add(item.StaffID, row);
                }
                else
                {
                    row = (DataRow)htContainer[item.StaffID];
                }
                row["ToMoney"] = (decimal)row["ToMoney"] + (item.ToMoney.HasValue ? item.ToMoney.Value : 0);
                //Insert con
                DataRow newRow = dtSource.NewRow();
                dtSource.Rows.InsertAt(newRow, dtSource.Rows.IndexOf(row) + 1);
                newRow["CreateDate"] = item.CreateDate;
                newRow["ProductNo"] = item.ProductNo;
                newRow["ProductName"] = item.ProductName;
                newRow["Quantity"] = item.Quantity;
                newRow["UnitPrice"] = item.UnitPrice;
                newRow["ToMoney"] = item.ToMoney;
                //Cộng dồn vào dòng cộng
                rowTongCong["ToMoney"] = (decimal)rowTongCong["ToMoney"] + (item.ToMoney.HasValue ? item.ToMoney.Value : 0);
            }
            dtSource.Rows.Add(rowTongCong);
            gridControl.DataSource = dtSource;
            #region Định dạng bảng
            int i = 0;
            foreach (DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn col in bandedGridView1.Columns)
            {
                DevExpress.XtraGrid.Views.BandedGrid.GridBand _band = new DevExpress.XtraGrid.Views.BandedGrid.GridBand();
                col.VisibleIndex = i++;
                switch (col.FieldName)
                {
                    case "style": continue;
                    case "stt":
                        col.Width = 55;
                        _band.Caption = "No";
                        col.AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                        _band.Columns.Add(col);
                        bandedGridView1.Bands.Add(_band);
                        break;
                    case "CreateDate":
                        col.Width = 100;
                        _band.Caption = "CreateDate";
                        col.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                        col.DisplayFormat.FormatString = "dd/MM/yyyy";
                        _band.Columns.Add(col);
                        bandedGridView1.Bands.Add(_band);
                        break;
                    case "ProductNo":
                        col.Width = 200;
                        _band.Caption = "Product No";
                        _band.Columns.Add(col);
                        bandedGridView1.Bands.Add(_band);
                        break;
                    case "ProductName":
                        col.Width = 350;
                        _band.Caption = "Product Name";
                        _band.Columns.Add(col);
                        bandedGridView1.Bands.Add(_band);
                        break;
                    case "Quantity":
                        col.Width = 120;
                        _band.Caption = "Quantity Receiving";
                        _band.Columns.Add(col);
                        bandedGridView1.Bands.Add(_band);
                        break;
                    case "UnitPrice":
                        col.Width = 120;
                        _band.Caption = "UnitPrice Receiving";
                        _band.Columns.Add(col);
                        bandedGridView1.Bands.Add(_band);
                        break;
                    case "ToMoney":
                        col.Width = 120;
                        _band.Caption = "ToMoney";
                        _band.Columns.Add(col);
                        bandedGridView1.Bands.Add(_band);
                        break;
                }
            }
            FormatXtraGridView(gridControl);
            DinhDangGridBandVaColumns(bandedGridView1);
            bandedGridView1.OptionsPrint.AutoWidth = true;
            bandedGridView1.BandPanelRowHeight = 30;
            FormatNumberInGridView(bandedGridView1);
            #endregion

        }
        protected override void PreviewReport()
        {
            try
            {
                string time = "From " + dtNgayBatDau.DateTime.ToString("dd/MM/yyyy") + " to " + dtNgayKetThuc.DateTime.ToString("dd/MM/yyyy");
                PrintWithHeaderAndFooter(true, gridControl, true, true, "\nPRODUCT RECEIVED REPORT", time, "", "", "", "");
            }
            catch { }
        }
    }
}
